package org.jeecg.modules.wmhb.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.modules.wmhb.entity.WmTaskbook;
import org.jeecg.modules.wmhb.service.IWmTaskbookService;
import org.jeecg.modules.wmhb.vo.WmFlowDeptVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wmhb.entity.WmFlowDept;
import org.jeecg.modules.wmhb.entity.WmTaskbookFlow;
import org.jeecg.modules.wmhb.vo.WmTaskbookFlowPage;
import org.jeecg.modules.wmhb.service.IWmTaskbookFlowService;
import org.jeecg.modules.wmhb.service.IWmFlowDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.system.api.ISysBaseAPI;

/**
 * @Description: 任务书流程表
 * @Author: jeecg-boot
 * @Date: 2024-09-05
 * @Version: V1.0
 */
@Api(tags = "任务书流程表")
@RestController
@RequestMapping("/wmhb/wmTaskbookFlow")
@Slf4j
public class WmTaskbookFlowController {
    @Autowired
    private IWmTaskbookFlowService wmTaskbookFlowService;
    @Autowired
    private IWmFlowDeptService wmFlowDeptService;
    @Autowired
    ISysBaseAPI sysBaseApi;
    @Autowired
    private IWmTaskbookService wmTaskbookService;


    /**
     * 分页列表查询
     *
     * @param wmTaskbookFlow
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "任务书流程表-分页列表查询")
    @ApiOperation(value = "任务书流程表-分页列表查询", notes = "任务书流程表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WmTaskbookFlow>> queryPageList(WmTaskbookFlow wmTaskbookFlow,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        QueryWrapper<WmTaskbookFlow> queryWrapper = QueryGenerator.initQueryWrapper(wmTaskbookFlow, req.getParameterMap());
        Page<WmTaskbookFlow> page = new Page<WmTaskbookFlow>(pageNo, pageSize);
        IPage<WmTaskbookFlow> pageList = wmTaskbookFlowService.page(page, queryWrapper);

        List<WmTaskbookFlow> wmTaskbookFlowList = new ArrayList<>();
        for (WmTaskbookFlow taskbookFlow : pageList.getRecords()) {
            //1、通过任务书流程ID获取流程部门列表
            List<WmFlowDept> wmFlowDeptList = wmFlowDeptService.selectByMainId(taskbookFlow.getId());

            //2、流程部门列表转换成视图对象
            List<WmFlowDeptVo> wmFlowDeptVoList = new ArrayList<>();
            for (WmFlowDept wmFlowDept : wmFlowDeptList) {
                WmFlowDeptVo wmFlowDeptVo = new WmFlowDeptVo();
                BeanUtils.copyProperties(wmFlowDept, wmFlowDeptVo);
                SysDepartModel sysDepartModel = sysBaseApi.selectAllById(wmFlowDept.getDeptId());
                wmFlowDeptVo.setDeptName(sysDepartModel.getDepartName());
                wmFlowDeptVoList.add(wmFlowDeptVo);
            }

            //3、添加到新的任务书流程列表
            taskbookFlow.setWmFlowDeptVoList(wmFlowDeptVoList);
            wmTaskbookFlowList.add(taskbookFlow);
        }
        pageList.setRecords(wmTaskbookFlowList);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param wmTaskbookFlowPage
     * @return
     */
    @AutoLog(value = "任务书流程表-添加")
    @ApiOperation(value = "任务书流程表-添加", notes = "任务书流程表-添加")
    @RequiresPermissions("wmhb:wm_taskbook_flow:add")
    @PostMapping(value = "/add")
    @Transactional
    public Result<String> add(@RequestBody WmTaskbookFlowPage wmTaskbookFlowPage) {
        WmTaskbookFlow wmTaskbookFlow = new WmTaskbookFlow();
        BeanUtils.copyProperties(wmTaskbookFlowPage, wmTaskbookFlow);
        wmTaskbookFlowService.saveMain(wmTaskbookFlow, wmTaskbookFlowPage.getWmFlowDeptList());
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wmTaskbookFlowPage
     * @return
     */
    @AutoLog(value = "任务书流程表-编辑")
    @ApiOperation(value = "任务书流程表-编辑", notes = "任务书流程表-编辑")
    @RequiresPermissions("wmhb:wm_taskbook_flow:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @Transactional
    public Result<String> edit(@RequestBody WmTaskbookFlowPage wmTaskbookFlowPage) {
        WmTaskbookFlow wmTaskbookFlow = new WmTaskbookFlow();
        BeanUtils.copyProperties(wmTaskbookFlowPage, wmTaskbookFlow);
        WmTaskbookFlow wmTaskbookFlowEntity = wmTaskbookFlowService.getById(wmTaskbookFlow.getId());
        if (wmTaskbookFlowEntity == null) {
            return Result.error("未找到对应数据");
        }
        wmTaskbookFlowService.updateMain(wmTaskbookFlow, wmTaskbookFlowPage.getWmFlowDeptList());
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "任务书流程表-通过id删除")
    @ApiOperation(value = "任务书流程表-通过id删除", notes = "任务书流程表-通过id删除")
    @RequiresPermissions("wmhb:wm_taskbook_flow:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        wmTaskbookFlowService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "任务书流程表-批量删除")
    @ApiOperation(value = "任务书流程表-批量删除", notes = "任务书流程表-批量删除")
    @RequiresPermissions("wmhb:wm_taskbook_flow:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wmTaskbookFlowService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "任务书流程表-通过id查询")
    @ApiOperation(value = "任务书流程表-通过id查询", notes = "任务书流程表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmTaskbookFlow> queryById(@RequestParam(name = "id", required = true) String id) {
        WmTaskbookFlow wmTaskbookFlow = wmTaskbookFlowService.getById(id);
        if (wmTaskbookFlow == null) {
            return Result.error("未找到对应数据");
        }

        //1、通过任务书流程ID获取流程部门列表
        List<WmFlowDept> wmFlowDeptList = wmFlowDeptService.selectByMainId(id);

        //2、流程部门列表转换成视图对象
        List<WmFlowDeptVo> wmFlowDeptVoList = new ArrayList<>();
        for (WmFlowDept wmFlowDept : wmFlowDeptList) {
            WmFlowDeptVo wmFlowDeptVo = new WmFlowDeptVo();
            BeanUtils.copyProperties(wmFlowDept, wmFlowDeptVo);
            SysDepartModel sysDepartModel = sysBaseApi.selectAllById(wmFlowDept.getDeptId());
            wmFlowDeptVo.setDeptName(sysDepartModel.getDepartName());
            wmFlowDeptVoList.add(wmFlowDeptVo);
        }
        wmTaskbookFlow.setWmFlowDeptVoList(wmFlowDeptVoList);

        return Result.OK(wmTaskbookFlow);

    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "任务书流程部门列表通过主表ID查询")
    @ApiOperation(value = "任务书流程部门列表主表ID查询", notes = "任务书流程部门列表-通主表ID查询")
    @GetMapping(value = "/queryWmFlowDeptByMainId")
    public Result<List<WmFlowDeptVo>> queryWmFlowDeptListByMainId(@RequestParam(name = "id", required = true) String id) {
        List<WmFlowDept> wmFlowDeptList = wmFlowDeptService.selectByMainId(id);
        List<WmFlowDeptVo> wmFlowDeptVoList = new ArrayList<>();
        for (WmFlowDept wmFlowDept : wmFlowDeptList) {
            WmFlowDeptVo wmFlowDeptVo = new WmFlowDeptVo();
            BeanUtils.copyProperties(wmFlowDept, wmFlowDeptVo);
            SysDepartModel sysDepartModel = sysBaseApi.selectAllById(wmFlowDept.getDeptId());
            wmFlowDeptVo.setDeptName(sysDepartModel.getDepartName());
            wmFlowDeptVoList.add(wmFlowDeptVo);
        }
        return Result.OK(wmFlowDeptVoList);
    }

    /**
     * 通过任务书id查询
     *
     * @param wmTaskbookId
     * @return
     */
    //@AutoLog(value = "任务书流程部门列表通过任务书ID查询")
    @ApiOperation(value = "任务书流程部门列表通过任务书ID查询", notes = "任务书流程部门列表通过任务书ID查询")
    @GetMapping(value = "/queryWmFlowDeptListByTaskbookId")
    public Result<List<WmFlowDeptVo>> queryWmFlowDeptListByTaskbookId(@RequestParam(name = "wmTaskbookId", required = true) String wmTaskbookId) {
        WmTaskbook wmTaskbook = wmTaskbookService.getById(wmTaskbookId);
		List <WmFlowDept> wmFlowDeptList = wmFlowDeptService.selectByMainId(wmTaskbook.getWmTaskbookFlowId());
        List<WmFlowDeptVo> wmFlowDeptVoList = new ArrayList<>();
        for (WmFlowDept wmFlowDept : wmFlowDeptList) {
            WmFlowDeptVo wmFlowDeptVo = new WmFlowDeptVo();
            BeanUtils.copyProperties(wmFlowDept, wmFlowDeptVo);
            SysDepartModel sysDepartModel = sysBaseApi.selectAllById(wmFlowDept.getDeptId());
            wmFlowDeptVo.setDeptName(sysDepartModel.getDepartName());
            wmFlowDeptVoList.add(wmFlowDeptVo);
        }
        return Result.OK(wmFlowDeptVoList);
    }

}
